﻿using ScottPlot.Plottables;
using ScottPlot;
using ScottPlot.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DetectScars.common;
using ScottPlot.PlotStyles;

namespace DetectScars.Menus
{
    public partial class PlotForm : Form
    {
        private TabControl tabControl;
        private AnchorStyles anchorstyles;
        private DockStyle dock;
        private Size size;
        private Point point;
        public PlotForm(FormsPlot formsPlot, TabControl tab)
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            anchorstyles = formsPlot.Anchor;
            dock = formsPlot.Dock;
            size = formsPlot.Size;
            point = formsPlot.Location;
            formsPlot.Dock = DockStyle.Fill;
            this.Controls.Add(formsPlot);
            this.Size = size;
            this.tabControl = tab;
        }
        public PlotForm(FormsPlot formsPlot)
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;

            formsPlot.Refresh();

            FormsPlot formsPlot1 = new FormsPlot();
            this.Controls.Add(formsPlot1);
            formsPlot1.Anchor = formsPlot.Anchor;

            formsPlot1.Dock = DockStyle.Fill;
            this.Size = formsPlot.Size;

            DraggableAxisLines draggableAxisLines = new DraggableAxisLines();
            draggableAxisLines.setFormsPlot(formsPlot1, "");


            formsPlot1.Plot.Axes.Bottom.Min = formsPlot.Plot.Axes.Bottom.Min;
            formsPlot1.Plot.Axes.Bottom.Max = formsPlot.Plot.Axes.Bottom.Max;
            formsPlot1.Plot.Axes.Left.Min = formsPlot.Plot.Axes.Left.Min;
            formsPlot1.Plot.Axes.Left.Max = formsPlot.Plot.Axes.Left.Max;

            var draggableVLineV = formsPlot1.Plot.Add.VerticalLine(x: (formsPlot1.Plot.Axes.Bottom.Max + formsPlot1.Plot.Axes.Bottom.Min)/2, width: 1, color: Colors.Red);
            draggableVLineV.IsDraggable = true;
            if (formsPlot.Name== "formsPlot2" || formsPlot.Name == "formsPlot4")
            {
                ;
            }
            else
            {
                var draggableVLineH = formsPlot1.Plot.Add.HorizontalLine(y: (formsPlot1.Plot.Axes.Left.Min + formsPlot1.Plot.Axes.Left.Max) / 2, width: 1, color: Colors.Red);
                draggableVLineH.IsDraggable = true;
            }



            foreach (IPlottable plottable in formsPlot.Plot.PlottableList)
            {

                if (plottable is Signal signal_)
                {
                    formsPlot1.Plot.Add.Signal(signal_.Data);
                }
                else if (plottable is LinePlot line)
                {
                    formsPlot1.Plot.Add.Line(line.Line);
                }
                else if (plottable is Heatmap map)
                {
                    var hm1 = formsPlot1.Plot.Add.Heatmap(map.Intensities);
                    hm1.Position = map.Position;
                    hm1.Colormap = new ScottPlot.Colormaps.Jet();
                    formsPlot1.Plot.Add.ColorBar(hm1);
                    hm1.ManualRange = map.ManualRange;
                    formsPlot1.Plot.MoveToBottom(hm1);
                }
                else if (plottable is SignalXY signalXY)
                {
                    SignalXY singal = formsPlot1.Plot.Add.SignalXY(signalXY.Data);
                    singal.Data.Rotated = true;
                }
            }
            
            

            formsPlot1.Refresh();
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (tabControl == null)
            {
                base.OnFormClosing(e);
                return;
            }
            //如果程序最小化，先最大化
            Form main = tabControl.FindForm();
            if (main.WindowState == FormWindowState.Minimized)
            {
                main.WindowState = FormWindowState.Maximized;
                main.Show();
            }
            base.OnFormClosing(e);
            string name = "";
            // 将FormsPlot控件从弹出窗口移除
            foreach (Control control in this.Controls)
            {
                if (control is FormsPlot formsPlot_)
                {
                    name = formsPlot_.Name;
                    this.Controls.Remove(formsPlot_);
                    if (name == "formsPlot1")
                    {
                        tabControl.Controls[0].Controls.Add(formsPlot_);
                        
                    }
                    else if (name == "formsPlot2")
                    {
                        tabControl.Controls[1].Controls.Add(formsPlot_);
                        
                    }
                    else if (name == "formsPlot3")
                    {
                        tabControl.Controls[2].Controls.Add(formsPlot_);
                        
                    }
                    else if (name == "formsPlot4")
                    {
                        tabControl.Controls[1].Controls.Add(formsPlot_);
                        
                    }
                    else if (name == "formsPlot5")
                    {
                        tabControl.Controls[2].Controls.Add(formsPlot_);
                        
                    }
                    else if (name == "formsPlot6")
                    {
                        tabControl.Controls[2].Controls.Add(formsPlot_);
                        
                    }
                    formsPlot_.Anchor = anchorstyles;
                    formsPlot_.Dock = dock;
                    formsPlot_.Size = size;
                    formsPlot_.Location = point;
                    break;
                }
            }
            
        }
    }
}
